#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 50000

struct
{
    int u,v,l;
    char dir[3];
} node[MAX_N];
int f[MAX_N],rnkx[MAX_N],rnky[MAX_N];
int n,m;
int Find(int x)
{
    int result = x;
    if(x!=f[x])
    {
        result = Find(f[x]);
        rnkx[x] += rnkx[f[x]];
        rnky[x] += rnky[f[x]];
        f[x] = result;
    }
    return result;
}

int Union(int x,int y,int disx,int disy)
{
    int tx = Find(x);
    int ty = Find(y);
    f[tx] = x;
    rnkx[tx] = -rnkx[x];
    rnky[tx] = -rnky[x];
    f[x] = y;
    rnkx[x] = disx;
    rnky[x] = disy;
}

int dx(char dir[])
{
    if (dir[0]=='E') return 0;
    if (dir[0]=='W') return 0;
    if (dir[0]=='N') return -1;
    if (dir[0]=='S') return 1;
}

int dy(char dir[])
{
    if (dir[0]=='E') return 1;
    if (dir[0]=='W') return -1;
    if (dir[0]=='N') return 0;
    if (dir[0]=='S') return 0;
}

void print(){
    for (int i = 1 ; i <= n ; i++){
        printf("%2d ",i);
    }
    printf("\n");
    for (int i = 1 ; i <= n ; i++){
        printf("%2d ",f[i]);
    }
    printf("\n");
    for (int i = 1 ; i <= n ; i++){
        printf("%2d ",rnkx[i]);
    }
    printf("\n");
    for (int i = 1 ; i <= n ; i++){
        printf("%2d ",rnky[i]);
    }
    printf("\n==========================================\n");

}

int main()
{
  //  freopen("1984.txt","r",stdin);

    cin >> n >> m;
    for (int i = 1 ; i <= m ; i++)
    {
        scanf("%d%d%d%s",&node[i].u,&node[i].v,&node[i].l,node[i].dir);
    }

    for (int i = 0 ; i < n ; i ++)
    {
        f[i] = i;
        rnkx[i] = 0;
        rnky[i] = 0;
    }

    int q;
    scanf("%d",&q);
    int u,v,t;
    int j = 1;
    int ux,uy,vx,vy;
    while (q--)
    {
        scanf("%d%d%d",&u,&v,&t);
        while (j<=t) {
                Union(node[j].u,
                      node[j].v,
                      dx(node[j].dir)*node[j].l,
                      dy(node[j].dir)*node[j].l
                      );
                j++;
            //    print();
        }

        if (Find(u) != Find(v)) printf("-1\n");
        else
        {
            ux = rnkx[u] , uy = rnky[u];
            vx = rnkx[v] , vy = rnky[v];
            printf("%d\n",abs(ux-vx)+abs(uy-vy));
        }
    }
    return 0;
}
